package com.SmartCampus.system.controller;

import com.SmartCampus.system.annotation.AuthCheck;
import com.SmartCampus.system.common.BaseResponse;
import com.SmartCampus.system.common.DeleteRequest;
import com.SmartCampus.system.common.ErrorCode;
import com.SmartCampus.system.common.ResultUtils;
import com.SmartCampus.system.constant.UserConstant;
import com.SmartCampus.system.exception.BusinessException;
import com.SmartCampus.system.exception.ThrowUtils;
import com.SmartCampus.system.model.dto.attendance.AttendanceAddRequest;
import com.SmartCampus.system.model.dto.attendance.AttendanceQueryRequest;
import com.SmartCampus.system.model.entity.Attendance;
import com.SmartCampus.system.model.vo.AttendanceVO;
import com.SmartCampus.system.service.AttendanceService;
import com.SmartCampus.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 考勤接口
 *
 * @author wangliang
 */
@RestController
@RequestMapping("/attendance")
@Slf4j
@Api(tags = "考勤接口")
public class AttendanceController {

    @Resource
    private AttendanceService attendanceService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建考勤（签到、签退）
     *
     * @param attendanceAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "创建考勤（签到、签退）")
    public BaseResponse<Integer> addAttendance(@RequestBody AttendanceAddRequest attendanceAddRequest, HttpServletRequest request) {
        Attendance attendance = new Attendance();
        BeanUtils.copyProperties(attendanceAddRequest, attendance);
        attendance.setStudentId(userService.getLoginUser(request).getId());
        boolean result = attendanceService.save(attendance);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(attendance.getId());
    }

    /**
     * 删除考勤（仅管理员）
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除考勤（仅管理员）")
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id 不能为空");
        }
        boolean result = attendanceService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取考勤列表（仅管理员）
     *
     * @param attendanceQueryRequest
     * @return
     */
    @PostMapping("/get/all")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "分页获取全部考勤列表（仅管理员）")
    public BaseResponse<Page<AttendanceVO>> listAttendanceByPage(@RequestBody AttendanceQueryRequest attendanceQueryRequest) {
        long current = attendanceQueryRequest.getCurrent() == 0 ? 1 : attendanceQueryRequest.getCurrent();
        long size = attendanceQueryRequest.getPageSize() == 0 ? 10 : attendanceQueryRequest.getPageSize();
        Page<AttendanceVO> pageParam = new Page<>(current, size);
        Page<AttendanceVO> attendancePage = attendanceService.getAll(pageParam, attendanceQueryRequest);
        if (attendancePage == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "查询失败，请联系管理员");
        }
        return ResultUtils.success(attendancePage);
    }

    /**
     * 分页获取我的考勤列表
     *
     * @param attendanceQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/get/my")
    @ApiOperation(value = "分页获取我的考勤列表")
    public BaseResponse<Page<AttendanceVO>> listAttendanceByMy(@RequestBody AttendanceQueryRequest attendanceQueryRequest, HttpServletRequest request) {
        if (attendanceQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long userId = userService.getLoginUser(request).getId();
        long current = attendanceQueryRequest.getCurrent() == 0 ? 1 : attendanceQueryRequest.getCurrent();
        long size = attendanceQueryRequest.getPageSize() == 0 ? 10 : attendanceQueryRequest.getPageSize();

        Page<AttendanceVO> pageParam = new Page<>(current, size);
        Page<AttendanceVO> attendancePage = attendanceService.getMy(pageParam, attendanceQueryRequest, userId);
        if (attendancePage == null) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "查询失败，请联系管理员");
        }
        return ResultUtils.success(attendancePage);
    }

    // endregion
}
